<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends CI_Controller {

    /**
     * Crawl Weibo Content And Info By Keyword
     *
     */
    const SINA          = 'sina';
    const YUNYUN        = 'yunyun';
    const NETEASE       = 'netease';
    const SOHU          = 'sohu';
    const TENCENT       = 'tencent';
    const PLATFORM      = 'platform';
    const ADMIN         = 'soften';
    const ERROR_TAB     = 'error';
    const LOG_LEVEL_ERROR = 'error';
    const LOG_LEVEL_WARING = 'waring';
    const LOG_LEVEL_NOTICE = 'notice';
    public $_log_dir    = 'keyword';
    public $_result_dir = 'result';
    public $_return_data;

    public function __construct(){
        parent::__construct();
        $this->load->helper('MY_auth_verification');
        $this->load->library('error_code_library');
        $this->load->library('crawl_abstract');
        $this->load->database();
        $this->_return_data = $this->config->item('return_data');
    }

    public function index(){
		$this->get_users();
	}

    public function get_users() {
        $auth_verification_result = authentication($this, $this->input->post());
        if(is_array($auth_verification_result) && array_key_exists(self::ERROR_TAB,$auth_verification_result)) {
            echo json_encode($auth_verification_result);
            exit;
        } else {
            $this->crawl();
        }
    }

    private function crawl() {
        $this->load->library('crawl_keyword_library');

		$crawl_history = null;//$this->crawl_keyword_library->find_in_cache();

        $this->_log_dir = $this->_log_dir.'/'.trim($this->input->post('platform'));
        $this->_result_dir = $this->_log_dir.'/'.$this->_result_dir;

        if(is_null($crawl_history)) {
            log_message(self::LOG_LEVEL_NOTICE, 'Crawl BY '.json_encode($this->input->post()), FALSE, $this->_log_dir);
            $platform = trim($this->input->post(self::PLATFORM));
            $soften = trim($this->input->post("soften"));
            
            $html = trim($this->input->post("html"));

            if($platform == self::SINA) {
            	$result = $this->sina_crawl();
            } else {
                $result = json_encode($this->error_code_library->getError('system', 'params', 'no_platform', $platform));
                log_message(self::LOG_LEVEL_ERROR, 'NO Result: '.$result, FALSE, $this->_log_dir);
                echo $result;
            }

            if(is_null($result) || array_key_exists(self::ERROR_TAB, $result) == FALSE) {
                if(array_key_exists('data', $result)) {
                    $result_string = json_encode($this->_return_data);
                } else {
                    $this->_return_data['data'] = $result;
                    $result_string = json_encode($this->crawl_keyword_library->return_utf8_code($this->_return_data));
                    //不用缓存了
                    //$this->crawl_keyword_library->save_crawl_result($result_string);
                }

                log_message(self::LOG_LEVEL_NOTICE, 'Return Result Crawl String: '.$result_string, FALSE, $this->_result_dir);

                echo $result_string;
            } else {
            	//如果是soften自己使用的..则直接返回原始错误信息
                if($this->input->post(self::ADMIN) === FALSE  && $this->input->get(self::ADMIN) === FALSE) {
                    $this->_return_data['error'] = $this->error_code_library->getError('system', 'common', 'failed');
                } else {
                    $this->_return_data['error'] = $result;
                }
                log_message(self::LOG_LEVEL_ERROR, 'Crawl Error: '.json_encode($this->crawl_keyword_library->return_utf8_code($this->_return_data)), FALSE, $this->_log_dir);
                echo json_encode($this->_return_data);
            }
        } else {
            log_message(self::LOG_LEVEL_NOTICE, 'Return Crawl String From Cache: '.$crawl_history, FALSE, $this->_result_dir);
            echo $crawl_history;
        }
    }

    private function sina_crawl() {
        $this->load->library('Crawl_sina_user_from_phone_library');
        $html_crawl_result = $this->crawl_sina_user_from_phone_library->crawl(true);
        if(array_key_exists(self::ERROR_TAB, $html_crawl_result) === FALSE && $html_crawl_result != FALSE) {
            log_message(self::LOG_LEVEL_NOTICE, 'Crawl From Sina HTML Finish And Result Is: '.json_encode($this->crawl_keyword_library->return_utf8_code($html_crawl_result)), FALSE, $this->_result_dir);
            return $html_crawl_result;
        } else if(array_key_exists(self::ERROR_TAB, $html_crawl_result) && FALSE == $this->crawl_abstract->not_illegal_error($html_crawl_result)) {
            return $html_crawl_result;
        }else {
            log_message(self::LOG_LEVEL_ERROR, 'Crawl From Sina HTML Failed: '.json_encode($this->crawl_keyword_library->return_utf8_code($html_crawl_result)), FALSE, $this->_log_dir);
           
            return $html_crawl_result;
        }
    }

    public function test() {
        $this->load->helper('url');
        $this->load->library('table');

        $post_data = $this->input->post();
        
        if($post_data['password'] != "112358s"){
        	echo "hehe";
        	exit;
        }
        $post_data['platform'] = "sina";
        $post_data['uid'] = $this->input->post('uid') !== FALSE ? trim($this->input->post('uid')):"3970734335";
        $post_data['method'] = "POST";
        $post_data['token'] = "9f7ffa5b38e13b1843eeffcc3ba06565";
        $post_data['page']  = $this->input->post('page') !== FALSE ? trim($this->input->post('page')) : 1;
        $post_data['soften'] = "yes";

        //测试接口..添加调试信息
        if($this->input->post('soften_admin_debug')) {
        	$soften_admin_debug = true;
        	$post_data['soften_admin_debug'] = $soften_admin_debug;
        }

        //$post_data = http_build_query($post_data);
        if($this->config->item('test_use_proxy') == TRUE) {
            $url= $this->config->item('test_proxy_add')."users/index";
        } else {
            $url= site_url("users/index");
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL,$url);

        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $result = curl_exec($ch);
        if ($result === FALSE) {
            echo "CURL Error In Test Connect API: " . curl_error($ch). " TO ".$url;
        }
        curl_close($ch);

        //测试接口..添加调试信息
        if($soften_admin_debug){
        	var_dump($result);
        	return ;
        }

        $wbfield = array(
            '微博ID', '微博信息内容', '微博创建时间', '微博来源', '转', '评', '好', '转载的微博ID', '转载的微博用户ID', '转载的微博用户名', '详情URL',
            '缩略图片地址', '中等尺寸图片地址', '原始图片地址', '增加时间', '渠道ID抓取渠道', '用户ID', '用户空间URL', '昵称', '描述', '头像', '粉丝数', '关注数', '是否加V'
        );

        if(is_null($result) ) {
            echo '无数据';
        } else {
            $result_data = json_decode($result, TRUE);
            if(is_null($result_data)) {
                echo '返回结果解析失败';die;
            }

            if(array_key_exists(self::ERROR_TAB, $result_data) && !empty($result_data[self::ERROR_TAB])) {
                echo '抓取错误<br/>';
                var_dump($result_data);
            } elseif(array_key_exists('data', $result_data) && is_null($result_data['data'])) {
                echo '无数据<br/>';
                var_dump($result_data);
            }else {
                echo '<head><meta charset="utf-8"><title>'.$post_data['platform'].'---'.$post_data['word'].'---'.$post_data['page'].'</title><style>*{font-size:12px}</style></head>';
                $tmpl = array ( 'table_open'  => '<table border="1" cellpadding="0" cellspacing="0" class="mytable">' );
                echo count($result_data['data']).'条记录';
                $this->table->set_template($tmpl);
                $this->table->set_heading($wbfield);
                foreach($result_data['data'] as $row) {
                	
                	$row = $row['mblog'];
                	
                    $data = array();
                    $data[] = $row['id'];
                    $data[] = $row['text'];
                    $data[] = $row['created_timestamp'];
                    $data[] = $row['source'];
                    $data[] = $row['reposts_count'];
                    $data[] = $row['comments_count'];
                    $data[] = $row['attitudes_count'];
                    $data[] = $row['retweeted_status']['id'];
                    $data[] = $row['retweeted_status']['user']['id'];
                    $data[] = $row['retweeted_status']['user']['screen_name'];
                    $data[] = "<a href='http://weibo.com/{$row['user']['id']}/{$row['bid']}' target='_blank'>http://weibo.com/{$row['user']['id']}/{$row['bid']}</a>";
                    $data[] = $row['thumbnail_pic'];
                    $data[] = $row['bmiddle_pic'];
                    $data[] = $row['original_pic'];
                    $data[] = $row['addtime'];
                    $data[] = $row['source_myid'];
                    $data[] = $row['user']['id'];
                    $data[] = empty($row['user']['profile_url'])? '': "<a href='http://weibo.com{$row['user']['profile_url']}' target='_blank'>http://weibo.com{$row['user']['profile_url']}</a>";
                    $data[] = $row['user']['screen_name'];
                    $data[] = $row['user']['description'];
                    $data[] = $row['user']['profile_image_url'];
                    $data[] = $row['user']['followers_count'];
                    $data[] = $row['user']['fansNum'];
                    $data[] = $row['user']['verified'];
                    $this->table->add_row($data);
                }
                echo $this->table->generate();
            }
        }

    }
}
